﻿using MaterialDesignThemes.Wpf;
using Prism.Mvvm;

namespace Compass.Wpf.ViewModels.Dialogs;

public class JobCardViewModel : BindableBase, IDialogHostAware
{
    #region ctor
    private readonly IContainerProvider _provider;
    public JobCardViewModel(IContainerProvider provider)
    {
        _provider = provider;
        SaveCommand = new DelegateCommand(PrintJobCard);//使用Save当作执行打印操作
        CancelCommand =new DelegateCommand(Cancel);
        ExecuteCommand = new DelegateCommand<string>(Execute);
    }

    
    public string DialogHostName { get; set; }
    public DelegateCommand SaveCommand { get; set; }
    public DelegateCommand CancelCommand { get; set; }
    public DelegateCommand<string> ExecuteCommand { get; set; }
    #endregion

    #region 属性
    private ModuleDto? moduleDto;
    public ModuleDto? ModuleDto
    {
        get => moduleDto;
        set { moduleDto = value; RaisePropertyChanged(); }
    }
    #endregion
    /// <summary>
    /// 打印JobCard
    /// </summary>
    private void PrintJobCard()
    {
        var printsService = _provider.Resolve<IPrintsService>();
        Task.Run(async () => { await printsService.PrintOneJobCardAsync(ModuleDto); });
        Cancel();//打印完成后关闭dialog
    }

    #region 执行批量操作动作，弹出批量操作弹窗
    private void Execute(string obj)
    {
        switch (obj)
        {
            case "PrintFinal": PrintFinal(); break;
            case "PrintEnFinal": PrintEnFinal(); break;
            case "PrintScreenShot": PrintScreenShot(); break;
            case "PrintEnScreenShot": PrintEnScreenShot(); break;
        }
    }
    /// <summary>
    /// 打印截图页
    /// </summary>
    private void PrintFinal()
    {
        var printsService = _provider.Resolve<IPrintsService>();
        Task.Run(async () => { await printsService.PrintOneFinalAsync(ModuleDto); });
        Cancel();//打印完成后关闭dialog
    }
    private void PrintEnFinal()
    {
        var printsService = _provider.Resolve<IPrintsService>();
        Task.Run(async () => { await printsService.PrintOneEnFinalAsync(ModuleDto); });
        Cancel();//打印完成后关闭dialog
    }

    /// <summary>
    /// 打印最终检验
    /// </summary>
    private void PrintScreenShot()
    {
        var printsService = _provider.Resolve<IPrintsService>();
        Task.Run(async () => { await printsService.PrintOneScreenShotAsync(ModuleDto); });
        Cancel();//打印完成后关闭dialog
    }
    private void PrintEnScreenShot()
    {
        var printsService = _provider.Resolve<IPrintsService>();
        Task.Run(async () => { await printsService.PrintOneEnScreenShotAsync(ModuleDto); });
        Cancel();//打印完成后关闭dialog
    }
    # endregion

    private void Cancel()
    {
        if (DialogHost.IsDialogOpen(DialogHostName))
            //取消时只返回No，告知操作结束
            DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
    }
    public void OnDialogOpen(IDialogParameters parameters)
    {
        ModuleDto = parameters.ContainsKey("Value") ? parameters.GetValue<ModuleDto>("Value") : null;
    }
}